package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApBehaviorLikesService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.article.pojos.ApUser;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.treadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApBehaviorLikesServiceImpl  implements ApBehaviorLikesService {
   @Autowired
    MongoTemplate mongoTemplate;
    /**
     * 点赞取消点赞
     * @param dto
     * @return
     */

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult LikesOrDislikes(LikesBehaviorDTO dto) {
        //根据当前登录用户id查询行为实体对象
        ApUser user = AppThreadLocalUtils.getUser();
        //用户不存在就发送给前端未登录
       if (user.getId() ==0){
           CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"用户需要登录才能点赞");
       }
        //根据当前登录的用户id查询行为实体的对象
        //根据以前写的查询实体类的方法
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());

       if (apBehaviorEntry ==null){
           CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"查询不到实体类");
       }
       //判断是否重复点赞
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));

        ApLikesBehavior entry = mongoTemplate.findOne(query, ApLikesBehavior.class);

//        if(dto.getOperation().intValue() == 0 && entry!=null){
//            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"请勿重复点赞");
//        }
        //开始点赞或取消点赞
        if (dto.getOperation().intValue() == 0 ){
            //开始点赞
            entry = new ApLikesBehavior();
            entry.setEntryId(apBehaviorEntry.getId());
            entry.setArticleId(dto.getArticleId());
            entry.setType((short)0);
            entry.setOperation((short)0);
            entry.setCreatedTime(new Date());
            mongoTemplate.save(entry);
        }else {
            //删除
            mongoTemplate.remove(query,ApLikesBehavior.class);
        }


        //发送点赞消息
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        updateArticleMess.setAdd(dto.getType().intValue() == 0 ? 1 : -1 );
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE , JSON.toJSONString(updateArticleMess));
        log.info("点赞行为 发送消息成功");
        return ResponseResult.okResult();


    }
}
